package com.lcc.codehelper.parse;

import cn.hutool.core.util.StrUtil;
import com.lcc.codehelper.config.DBCloumn;
import com.lcc.codehelper.entity.Column;
import com.lcc.codehelper.entity.Table;
import com.lcc.codehelper.mapper.TableMapper;
import com.lcc.codehelper.util.InitialCapacityUtil;
import com.lcc.codehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TableParse {

    @Autowired
    private TableMapper tableMapper;

    public Table parseTable(String tableName,Boolean splitByEnt) {
        Table table = new Table();
        table.setAlias(StringUtil.tableNameUnderLine2CamelAndUpperHead(tableName,splitByEnt));
        table.setTableName(tableName);
        table.setPropertyName(StringUtil.tableNameUnderLine2CamelAndLowerHead(tableName,splitByEnt));
        table.setRemark(parseComment(tableName));
        List<Map> mapList = tableMapper.listTable(tableName);
        Map<String, String> mapColumn = parseColumnComment(tableName);
        List<Column> columnList = new ArrayList<>();
        for (Map map : mapList) {
            Column column = new Column();
            String columnName = (String) map.get(DBCloumn.COLUMN_NAME);
            String dataType = (String) map.get(DBCloumn.DATA_TYPE);
            column.setProperty(StringUtil.underLine2Camel(columnName));
            column.setColumn(columnName);
            column.setJdbcType(JDBCTypeConvert.covertJdbcType(dataType));
            column.setRemark(mapColumn.get(columnName));
            column.setJavaType(JAVATypeConvert.covertJavaType(dataType));
            columnList.add(column);
        }
        table.setColumnList(columnList);
        return table;
    }

    private String parseComment(String tableName) {
        Map map = tableMapper.singleTableComment(tableName);
        if (map != null) {
            return StrUtil.blankToDefault((String)map.get(DBCloumn.COMMENTS),StrUtil.EMPTY);
        }
        return StrUtil.EMPTY;
    }

    private Map<String, String> parseColumnComment(String tableName) {
        List<Map> mapList = tableMapper.listColumnComment(tableName);
        Map<String, String> columnMap = new HashMap<>(InitialCapacityUtil.getInitialCapacity(mapList.size()));
        if (!CollectionUtils.isEmpty(mapList)) {
            for (Map map : mapList) {
                String column = (String) map.get(DBCloumn.COLUMN_NAME);
                String comment = StrUtil.blankToDefault((String)map.get(DBCloumn.COMMENTS),StrUtil.EMPTY);
                columnMap.put(column, comment);
            }
        }
        return columnMap;
    }

    public static void main(String[] args) {
        String tableName = "FINGARD_TRANSACTIONS_QT330001";
        System.out.println(StringUtil.underLine2Camel(tableName));
        System.out.println(StringUtil.tableNameUnderLine2CamelAndUpperHead(tableName,true));
        System.out.println(StringUtil.tableNameUnderLine2CamelAndLowerHead(tableName,true));
    }
}
